import {_decorator} from 'cc';
import {GameEvent} from '../event/GameEvent';
import EventCenter from '../event/EventCenter';
import {UOrder} from '../components/UOrder';
import {OrderPoolMgr} from './OrderPoolMgr';
import {CardPoolMgr} from './CardPoolMgr';
import {BaseCard} from '../models/Card';
import {ManagerBase} from './ManagerBase';
import {OrderSlotStatus} from '../GameDef';

const {ccclass, property} = _decorator;

@ccclass('OrderManager')
export default class OrderManager extends ManagerBase {

    orderUids: number[]         = [];
    @property([UOrder])
    private orderList: UOrder[] = [];

    onLoad() {
    }

    onDestroy() {
    }

    checkOrder(card: BaseCard) {
        for (let uid of this.orderUids) {
            if (uid < OrderSlotStatus.exist)
                continue;
            let orderData = OrderPoolMgr.getInstance().getOrderById(uid);
            if (!orderData.checkSuit)
                continue;
            let fitIndex = orderData.isFit(card);
            if (fitIndex >= 0)
                return [uid, fitIndex];
        }

        for (let uid of this.orderUids) {
            if (uid < OrderSlotStatus.exist)
                continue;
            let orderData = OrderPoolMgr.getInstance().getOrderById(uid);

            let fitIndex = orderData.isFit(card);
            if (fitIndex >= 0)
                return [uid, fitIndex];
        }
        return [];
    }

    onCardAdd(card: BaseCard, orderUid: number, fitIndex: number) {
        let orderInPool = OrderPoolMgr.getInstance().getOrderById(orderUid);
        let cardInPool  = CardPoolMgr.getInstance().getCardById(card.uid);
        let fixCardUid  = orderInPool.setCard(cardInPool);

        let index      = this.orderUids.indexOf(orderUid);
        let UOrder     = this.orderList[index];
        let isComplete = UOrder.AddCard(card, fitIndex);
        if (isComplete) {
            EventCenter.emit(GameEvent.OrderComplete);
        }
        if (fixCardUid > 0)
            return fixCardUid;
        return;
    }

    initUI() {
        for (let index = 0; index < this.orderList.length; index++) {
            let UOrder        = this.orderList[index];
            let uid           = this.orderUids[index];
            UOrder.isAdLocked = uid == OrderSlotStatus.adLock;
        }
    }

    initData() {
        this.orderUids = [OrderSlotStatus.empty, OrderSlotStatus.empty, OrderSlotStatus.adLock, OrderSlotStatus.adLock];
    }

    tryGetOrder() {

        for (let index = 0; index < this.orderList.length; index++) {
            let UOrder = this.orderList[index];
            if (UOrder.isAdLocked) continue;


            let uid = this.orderUids[index];
            if (uid > 0) {
                let orderInPool = OrderPoolMgr.getInstance().getOrderById(uid);
                if (!orderInPool.isFinished)
                    continue;
            }

            let orderInfo         = this.getNewOrder();
            this.orderUids[index] = orderInfo?.uid || OrderSlotStatus.empty;
            UOrder.init(orderInfo);
        }

    }

    private getNewOrder() {
        console.log('getNewOrder');
        return OrderPoolMgr.getInstance().getNextOrder();
    }
}